/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*  审核岗位调查审核分配任务
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('CaseAuditFindDispathCtrl', ['$scope','$http','$modal','CaseCommonService','WarningAddService','CaseAuditFindDispathService','BaseURL','Modal','FwaTabsetService','$stateParams','$state','WarningScreenService',
	                                function($scope,$http,$modal,CaseCommonService,WarningAddService,CaseAuditFindDispathService,baseUrl,Modal,FwaTabsetService,$stateParams,$state,WarningScreenService){
		
		 //事件对象
		 $scope.problemObj={};
		 
		 
		 $scope.problemtab=[ {label:'事件处理',id:0,url:'views/hcpms/audit/problemManagement/caseAduitFindDispathDealProblem.html'}];

		                 $scope.tabClicks = function(tab) {
		                     $scope.selectedTabs = tab;
		                     $scope.tabIndex = tab.id;
		                 }
		                 $scope.tabClicks($scope.problemtab[0]);
		
		

		//初始化请求参数
		var params = function (){
			var problemNo = "";
			if($stateParams.problemNo && !angular.isUndefined($stateParams.problemNo) ){
				problemNo = $stateParams.problemNo;
				$scope.problemNo=problemNo;
				
			}
			var params = {
					problemNo:problemNo
			}
			return params;
		}
		
		
		/**
		 * 初始化医疗机构数据和医护人员信息和参保人信息
		 */
        $scope.medicalCode='';
		function initMedicalData(){
			var parameter=params();
			parameter.taskType='1';//事件
			CaseCommonService.loadMedicalInfoList(parameter).success(function(response){
				$scope.medicalData2=response.medicalBean.medicalList;
                if($scope.medicalData2.length>0)
                {
                    if($scope.medicalData2[0].medicalCode)
                    {
                        $scope.medicalCode=$scope.medicalData2[0].medicalCode;
                    }
                }
				$scope.staffData2=response.staffBean.staffList;
				$scope.insuranceData2=response.insuranceBean.insuranceList;
				
				CaseCommonService.initStaffAndInsuranceTempList($scope);
				//更新当前页面数据
     		   WarningScreenService.findPointCutTempScose(
     				   WarningAddService.warningMarks,$stateParams.problemNo,'02',
     				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
	    	 
			});
		}
		
		/**
		 * 初始化事件数据对象
		 */
        $scope.object={};
		function initProblemData(){
			var parameter=params();
			CaseCommonService.loadProblemData(parameter).success(function(response){
		    	 var obj=response.problemObj;
		    	 $scope.object=obj;	
				});
		}
		
		
		
		initProblemData();
		initMedicalData();
		initTempInfo(); //初始化暂存信息
		
		$scope.showDispatchButton=true; //默认展现分配按钮
		$scope.showConfirmButton=true; //默认展现确定按钮
		$scope.auditObj={}; //保存审核意见和审核意见描述对象
		$scope.treatmentAdvice={}; //映射事件的对象
		
	
		/**审核意见选择监听函数***/
		$scope.selectChangeFun=function(){
			
			if($scope.auditObj){
				var opinion=$scope.auditObj.opinion;
				if(opinion && opinion=='1'){ //同意
					$scope.showDispatchButton=true;
					$scope.showConfirmButton=false;
				}else{
					$scope.showDispatchButton=false;
					$scope.showConfirmButton=true;
				}
			}
			
		}

        var warningMarks = [];
		/**
		 * 返回事件
		 */
		$scope.dispatchBackClick=function(){
			WarningAddService.warningMarks = WarningAddService.warningMarkTemps;
            if($stateParams.urlFlag){
                var urlFlag =  $stateParams.urlFlag;
                FwaTabsetService.closeTab('problemCheck','workingArea.auditPost',{urlFlag:urlFlag});
            }else
            {
                $state.go('problemCheck.problemCheckList');
            }
		}
		
		/****初始化事件意见***/
		function initProblemOpinion(){
			var parameter={problemNo:$stateParams.problemNo};
			  CaseAuditFindDispathService.loadProblemDealOpinion(parameter).success(function(response){
				  if(response.treatmentOpinion){
					  var currentOpinion=response.treatmentOpinion.opinion;
					  var currentOpinionDesc='';
					  if(currentOpinion=='1'){ //需要调查
						  currentOpinionDesc='需要调查';
					  }else if(currentOpinion=='0'){ //不需要调查
						  currentOpinionDesc='无需调查';
					  }else if(currentOpinion=='2'){///转其他部门处理
						  currentOpinionDesc='转其他部门处理';
					  }
					  
					  
					  $scope.treatmentAdvice={
							  opinion:currentOpinionDesc,
							  opinionDesc:response.treatmentOpinion.opinionDesc,
							  dealDate:response.treatmentOpinion.dealDate,
							  displayName:response.treatmentOpinion.displayName
					  }
					  
				  }
				 
			    	        	 
					});
		}
		
	        /**
	         * 点击医疗机构量事件
	         * 
	         */
	        $scope.$on('fwaInvolve.medicalCountClick',function(){
	     	   CaseCommonService.viewMedicalDetail($scope);
	        });
	        
	       /**
	        * 点击医务人员总数量事件
	        */
	       $scope.$on('fwaInvolve.staffCountClick',function(){
	       	CaseCommonService.viewStaffList($scope);
	       });
	       
	       /**
	        * 点击参保人总数量事件
	        */
	       $scope.$on('fwaInvolve.insuranceCountClick',function(){
	       	CaseCommonService.viewInsurList($scope);
	       });
	       
		
		/****************一下处理 事件处理******************/
		
		initProblemOpinion(); //初始化事件中的处理意见
		
		/****确定事件函数*****/
		 $scope.loading = false;
		$scope.confirmFun=function(){
			if($scope.auditObj){
				
				 if(!$scope.auditObj.opinion){
					 Modal.alert("请选择审核意见");
						return;
				 }else if(!$scope.auditObj.opinionDesc){
					 Modal.alert("请填写意见说明!");
						return;
					 
				 }
			}else{
				Modal.alert("请选择审核意见");
				return;
			}
			
			
			var parameter={
					opinion:$scope.auditObj.opinion,
					opinionDesc:$scope.auditObj.opinionDesc,
					problemNo:$stateParams.problemNo,
					firstOpinionDesc:$scope.treatmentAdvice.opinionDesc,
                    warnMarkTempDetails:WarningAddService.warningMarks
			};
			 $scope.loading = true;
			CaseAuditFindDispathService.processConfirmOpinion(parameter).success(function(data){
                        if(data.state=='true')
                        {	
                        	$scope.loading = false;
                            Modal.alert(data.msg);
                            $scope.dispatchBackClick();
                        }else
                        {
                            Modal.alert(data.msg);
                        }
					});
			
			 $scope.saveReallyStaffAndInsuarnceInfo();
		}
        /**
         * 医疗机构扣分事件
         */
        $scope.$on('fwaInvolve.medicalDeduction',function(e,v){
            $scope.marking (1,v, v.medicalCode, v.orgType);
        });
        /**
         * 医务人员扣分事件
         */
        $scope.$on('fwaInvolve.staffDeduction',function(e,v){
            $scope.marking(2,v,$scope.medicalCode,'');
        });
        //扣分 mark：1.机构扣分，2.医务人员扣分，3参保人员扣分,data:数据集合
        $scope.marking = function(mark,data,medicalCode,orgType){
            var medicalCodeTemp=null;
            if(medicalCode&&!angular.isUndefined(medicalCode)){
                medicalCodeTemp = medicalCode;
            }
            if(mark != 1 && medicalCodeTemp == null){
                Modal.alert("请先增加医疗机构信息！");
                return null;
            }else{
                medicalCode = medicalCodeTemp;
            }
            markData(mark,data,medicalCode,orgType);//扣分数据参数加载
            var modalInstance = $modal.open({
                templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningDeductionDialog_view.html',
                backdrop:'static',
                size:'sm',
                scope: $scope,
//					  scope:scope,
                controller: 'WarningDeductionDialogCtrl'
            });
            //接收返回数据
            modalInstance.result.then(function(item) {
                WarningAddService.warningMarks = item;
                //更新当前页面数据
    		   	WarningScreenService.findPointCutTempScose(
    		   		item,$stateParams.problemNo,'02',
    		   	$scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
                
            });
        }
        //扣分数据参数加载
        var markData = function(mark,data,medicalCode,orgType){
            WarningAddService.mark = mark;
            WarningAddService.warningMarkData = {
                orgType:orgType,//0.机构，1.药店
                medicalCode:medicalCode,//medicalCode:所属机构编号
                code:data.selfCode,//code:编号
                name:data.name,//name:名称
                caseType:'02',//案件类型caseType：01.预警,02.稽核
                caseNo: $stateParams.problemNo,//事件编号
                happenDate: $scope.object.hearDate,//受理日期
                punishDateStart: $scope.object.problemStart,// 事件开始日期
                punishDateEnd: $scope.object.problemEnd,// 事件结束日期
                warningMarks:WarningAddService.warningMarks
            };
        }
         function checkParamter(){
			if($scope.auditObj){
				if(!$scope.auditObj.opinion){
					Modal.alert("请选择审核意见");
					 return false;
				}else {
					return true;
				}
			}else{
				return false;
			}
		}
         
         
         /**
          * 暂存函数
          */
         $scope.loading = false;
         $scope.tempOnClick=function(){
        	 if($scope.auditObj){
 				
				 if(!$scope.auditObj.opinion){
					 Modal.alert("请选择审核意见");
						return;
				 }else if(!$scope.auditObj.opinionDesc){
					 Modal.alert("请填写意见说明!");
						return;
					 
				 }
			}else{
				Modal.alert("请选择审核意见");
				return;
			}
             var parameter={
                 problemNo:$stateParams.problemNo,
                 type:'save',
                 opinion:$scope.auditObj.opinion,
                 opinionDesc:$scope.auditObj.opinionDesc,
                 firstOpinionDesc:$scope.treatmentAdvice.opinionDesc,
                 warnMarkTempDetails:WarningAddService.warningMarks
             };
             $scope.loading =true;
             CaseAuditFindDispathService.processOpinion(parameter).success(function(data){
                 var flag=data.flag;
                 if(flag){
                	 $scope.loading = false;
                     Modal.alert('暂存保存成功!');
                     WarningAddService.warningMarks = [];
                   //更新当前页面数据
            		   WarningScreenService.findPointCutTempScose(
            				   WarningAddService.warningMarks,$stateParams.problemNo,'02',
            				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
                 }
             });
             
             $scope.saveStaffAndInsuarnceInfo();
         }
         
         /**
          * 初始化暂存信息
          */
         function initTempInfo(){
        	var  parameter={
					problemNo:$stateParams.problemNo,
					type:'load'
				};
        	 $http.post(baseUrl + 'auditFindDispatch/processTempSave.do',parameter).success(function(response){
        		 if(response.auditOpinion){
        			 
  					$scope.auditObj.opinion=response.auditOpinion.opinion;
  					$scope.auditObj.opinionDesc=response.auditOpinion.opinionDesc;
  					var opinion=$scope.auditObj.opinion;
  					
  					if(opinion && opinion=='1'){ //同意
  						$scope.showDispatchButton=true;
  						$scope.showConfirmButton=false;
  					}else{
  						$scope.showDispatchButton=false;
  						$scope.showConfirmButton=true;
  					}
  				 }
        	 });
        	
        	 
         }
		
		/**
		 * 分配事件
		 */

		$scope.dispatchOnClick = function(){
            $scope.warnMarkTempDetails = WarningAddService.warningMarks;
			var result=checkParamter();
			if(!result){
				return;
			}
			var scope = $scope.$new();
			var modalInstance = $modal.open({
				      templateUrl: baseUrl+'views/hcpms/audit/problemManagement/caseAuditFindDispatchSelectUserDialog_view.html',
				      backdrop:'static',
					  size:'sm',
					  scope:scope,
					  controller: 'auditFindDispatchSelectUserModalCtrl'
				    });
				
		}
		
		/**
	        * 查看事件信息对象中案件来源对话框
	        */
	       $scope.$on('fwaAuditLeft.showModal',function(){
	    	   $scope.problemObj=$scope.object;
	    	   CaseCommonService.showProblemFromSourceDialog($scope);
	       });
	       
	       
	       /**
	        * 调新增医务人员的回调函数
	        *  parameter={
	         * taskCode:'',
	         * taskType:'',1事件,2案件
	         * targetType:'',对象类型(1-医疗机构2-医生3-参保人)
	         * targetCode:''
	         * }
	        */
	       $scope.callStaffFun=function(array){
	    	   CaseCommonService.addTempStaffList(array,$scope);
	       }
	       
	       /**
	        * 调新增参保人员的回调函数
	        */
	       $scope.callInsuranceFun=function(array){
	    	   CaseCommonService.addTempInsuranceListInfo(array,$scope);
	       }
	       
	       
	       /**
	        * 新增医护人员事件
	        */
	       $scope.$on('fwaInvolve.staffAdd',function(){
	    	   CaseCommonService.addStaffFun($scope);
	    	   
	       });
	       
	       
	       /**
	        * 新增参保人员事件
	        */
	       $scope.$on('fwaInvolve.insuranceAdd',function(){
	    	   CaseCommonService.addInsuranceFun($scope);
	    	   
	       });
	       
	       /**
	        * 医务人员删除
	        */
	       $scope.$on('fwaInvolve.staffDel',function(e,data){
	    	   Modal.confirm('确认删除医务人员'+data.name+'吗？',function(){
					WarningScreenService.deleteMark(WarningAddService.warningMarks,2,data);//mark:1.机构，2.医务人员，3.参保人
					 CaseCommonService.deleteTempStaffList(data,$scope);
				});
	    	   
	    	    
	         });
	       
	       /**
	        * 参保人员删除
	        */
	       $scope.$on('fwaInvolve.insuranceDel',function(e,data){
	    	   
	    	   Modal.confirm('确认删除参保人员'+data.name+'吗？',function(){
					WarningScreenService.deleteMark(WarningAddService.warningMarks,3,data);//mark:1.机构，2.医务人员，3.参保人
					 CaseCommonService.deleteTempInsuranceInfoList(data,$scope);
				});
	    	    
	         });
	       
	       
	       /**
	        * 对医务人员和参保人员的暂存信息保存
	        */
	       $scope.saveStaffAndInsuarnceInfo=function(){
	    	   $scope.saveType="temp";
	    	   saveStaffListInfo();
	    	   saveInsuranceInfo();
	       }
	       
	       
	       /**
	        * 对医务人员和参保人员的真实数据信息保存
	        */
	       $scope.saveReallyStaffAndInsuarnceInfo=function(){
	    	   $scope.saveType="save";
	    	   saveStaffListInfo();
	    	   saveInsuranceInfo();
	       }
	       
	       
		   /**
		    * 暂存的时候对医务人员进行的保存
		    */
	       function saveStaffListInfo(){
		     CaseCommonService.saveTempStaffListInfo($scope,'1','2',$stateParams.problemNo);
		   
	       }
	       
	       /**
		    * 暂存的时候对参保人员进行的保存
		    */
	       function saveInsuranceInfo(){
	    	   CaseCommonService.saveTempInsuranceListInfo($scope,'1','3',$stateParams.problemNo);
	       }
	       
		
		
	}]);
})();
